package com.yigerende.controller;

import com.yigerende.pojo.TUser;
import com.yigerende.pojo.UserVo;
import com.yigerende.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    //查询所有用户分页，layui需要按它的规则
    @GetMapping("findAll/{page}/{limit}")
    public UserVo findAll(@PathVariable("page") int page,@PathVariable("limit") int limit){
        Map map = new HashMap();
        map.put("start",(page-1)*limit);
        map.put("end",limit);
        UserVo userVo = new UserVo();
        userVo.setCode(0);
        userVo.setMsg("");
        userVo.setCount(userService.count());
        userVo.setData(userService.findAll(map));
        return userVo;
    }

    //增加
    @PostMapping("save")
    public int save(@RequestBody TUser tUser){
        //给当前时间
        tUser.setRegisterDate(new Date());
        return userService.save(tUser);
    }

    //删除
    @GetMapping("deleteById/{id}")
    public int deleteById(@PathVariable("id") Integer id){
        return userService.deleteById(id);
    }

}
